package com.yqg.protection.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.protection.common.Result;
import com.yqg.protection.common.interceptor.SysLog;
import com.yqg.protection.entity.FireComSense;
import com.yqg.protection.service.IFireComSenseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;

/**
 * FireComSenseController 前端控制器
 * @author zxf
 * @since 2024-09-11
 */
@RestController
@RequestMapping("/fire-com-sense")
public class FireComSenseController {

    @Resource
    private IFireComSenseService fireComSenseService;

    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
    @SysLog("新增或者修改消防常识")
    public Result save(@RequestBody FireComSense fireComSense) {
        fireComSense.setEnterDate(LocalDateTime.now());
        fireComSense.setIsDelete(0);
        return Result.success(fireComSenseService.saveOrUpdate(fireComSense));
    }
    /**
    *  删除
    */
    @GetMapping("/delete/{id}")
    @SysLog("删除消防常识")
    public Result delete(@PathVariable Integer id) {
        FireComSense fireComSense =fireComSenseService.getById(id);
        fireComSense.setIsDelete(1);
        return Result.success(fireComSenseService.saveOrUpdate(fireComSense));
    }
    /**
    *  查询所有
    */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<FireComSense> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
       List<FireComSense> list=  fireComSenseService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(fireComSenseService.getById(id));
    }
    /**
    *  分页查询
    */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           String name,
                           String startTime,
                           String endTime) {
        QueryWrapper<FireComSense> queryWrapper = new QueryWrapper<>();
        if (startTime != null && !startTime.equals("")) {
            queryWrapper.between("enter_date", startTime, endTime);
        }
        queryWrapper.and(qw -> qw.like("com_sense_name", name).or().like("enter_name",name));
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");
        return Result.success(fireComSenseService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    @ApiOperation("导出数据")
    @GetMapping("/export")
    public void export(HttpServletResponse response,
                       String name,
                       String startTime,
                       String endTime) {
        fireComSenseService.export(response, name, startTime, endTime);
    }

    @ApiOperation("导入数据")
    @PostMapping("/importData")
    @SysLog("导入消防常识")
    public void importData(MultipartFile file) {
        fireComSenseService.importData(file);
    }
}

